package com.dataaccess.dataaccess.web.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.dataaccess.dataaccess.common.base.BaseService;
import com.dataaccess.dataaccess.support.model.RespEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: YangYu
 * @ClassName: DeviceOpenElectricService
 * @Date: Created in 13:58 2021/11/29
 * @Description: 空开数据处理
 */
@Service
public class DeviceOpenElectricService extends BaseService {

    @Resource
    WaterPumpAsyncService waterPumpAsyncService;
    @Resource
    private AsyncService asyncService;
    /**
     * @Author YangYu
     * @Description 空开数据接收
     * @Date Created in 13:59 2021/11/29
     * @Param [jsonParam]
     */

    public RespEntity openElectric(JSONObject jsonParam) throws Exception {
        String code = jsonParam.getStr("code");
        String[] codes = code.split("0d0a0d0a");
        for (String c : codes){
            //空开地址码
            String add = c.substring(0,2);
            //功能码
            String functionCode = c.substring(2,4);
            //主机设备号
            String deviceNumber = c.substring(4,20);

            //报警数据
            if (functionCode.equals("13")){
                //短路报警值
                String shortCircuitAlarmValue = c.substring(65,69);
                //漏电报警值
                String leakageAlarmValue = c.substring(69,73);
                //A相过流报警值
                String aPhaseCurrentAlarmValue = c.substring(69,73);
                double aPhaseCurrentAlarmValueDouble  = hexToInt(aPhaseCurrentAlarmValue)/100.0;
                //B相过流报警值
                String bPhaseCurrentAlarmValue = c.substring(73,77);
                double bPhaseCurrentAlarmValueDouble  = hexToInt(bPhaseCurrentAlarmValue)/100.0;
                //C相过流报警值
                String cPhaseCurrentAlarmValue = c.substring(77,81);
                double cPhaseCurrentAlarmValueDouble  = hexToInt(cPhaseCurrentAlarmValue)/100.0;
                //A相过压报警值
                String aPhaseOvervoltageAlarmValue = c.substring(81,85);
                double aPhaseOvervoltageAlarmValueDouble  = hexToInt(aPhaseOvervoltageAlarmValue)/100.0;
                //B相过压报警值
                String bPhaseOvervoltageAlarmValue = c.substring(85,89);
                double bPhaseOvervoltageAlarmValueDouble  = hexToInt(bPhaseOvervoltageAlarmValue)/100.0;
                //C相过压报警值
                String cPhaseOvervoltageAlarmValue = c.substring(89,93);
                double cPhaseOvervoltageAlarmValueDouble  = hexToInt(cPhaseOvervoltageAlarmValue)/100.0;
                //A相欠压报警值
                String aPhaseUnderVoltageAlarmValue = c.substring(93,97);
                double aPhaseUnderVoltageAlarmValueDouble  = hexToInt(aPhaseUnderVoltageAlarmValue)/100.0;
                //B相欠压报警值
                String bPhaseUnderVoltageAlarmValue = c.substring(97,101);
                double bPhaseUnderVoltageAlarmValueDouble  = hexToInt(bPhaseUnderVoltageAlarmValue)/100.0;
                //C相欠压报警值
                String cPhaseUnderVoltageAlarmValue = c.substring(101,105);
                double cPhaseUnderVoltageAlarmValueDouble  = hexToInt(cPhaseUnderVoltageAlarmValue)/100.0;
                //A相温度报警值
                String aPhaseTemperatureAlarmValue = c.substring(117,121);
                double aPhaseTemperatureAlarmValueDouble  = hexToInt(aPhaseTemperatureAlarmValue)/10.0;
                //B相温度报警值
                String bPhaseTemperatureAlarmValue = c.substring(121,125);
                double bPhaseTemperatureAlarmValueDouble  = hexToInt(bPhaseTemperatureAlarmValue)/10.0;
                //C相温度报警值
                String cPhaseTemperatureAlarmValue = c.substring(125,129);
                double cPhaseTemperatureAlarmValueDouble  = hexToInt(cPhaseTemperatureAlarmValue)/10.0;

                List<JSONObject> mapList = new ArrayList<>();
                JSONObject jsonObject = new JSONObject();
                //报警类型0000002300010023
                String alarmTypeCode = c.substring(25,41);
                if (!alarmTypeCode.substring(0,4).equals("0000")){
                    //报警类型转二进制
                    String alarm = hexToBinaryString(alarmTypeCode.substring(0,4));
                    //漏电报警
                    if (alarm.charAt(0) == '1'){
                        jsonObject.put("alarmTypeName","漏电报警");
                        jsonObject.put("content","漏电报警 报警值:"+leakageAlarmValue);
                        mapList.add(jsonObject);
                    }
                    if (alarm.charAt(1) == '1'){

                    }
                    if (alarm.charAt(2) == '1'){
                        jsonObject = new JSONObject();
                        jsonObject.put("alarmTypeName","温度报警");
                        jsonObject.put("content","过温报警 报警值: A:"+aPhaseTemperatureAlarmValueDouble+"℃ B:"+bPhaseTemperatureAlarmValueDouble+"℃ C:"+cPhaseTemperatureAlarmValueDouble+"℃");
                        mapList.add(jsonObject);
                    }
                    if (alarm.charAt(3) == '1'){

                    }
                    if (alarm.charAt(4) == '1'){

                    }
                    if (alarm.charAt(5) == '1'){
                        jsonObject = new JSONObject();
                        jsonObject.put("alarmTypeName","欠压报警");
                        jsonObject.put("content","欠压报警 报警值: A:"+aPhaseUnderVoltageAlarmValueDouble+"V B:"+bPhaseUnderVoltageAlarmValueDouble+"V C:"+cPhaseUnderVoltageAlarmValueDouble+"V");
                        mapList.add(jsonObject);
                    }
                    if (alarm.charAt(6) == '1'){
                        jsonObject = new JSONObject();
                        jsonObject.put("alarmTypeName","过压报警");
                        jsonObject.put("content","过压报警 报警值: A:"+aPhaseOvervoltageAlarmValueDouble+"V B:"+bPhaseOvervoltageAlarmValueDouble+"V C:"+cPhaseOvervoltageAlarmValueDouble+"V");
                        mapList.add(jsonObject);
                    }
                    if (alarm.charAt(7) == '1'){
                        jsonObject = new JSONObject();
                        jsonObject.put("alarmTypeName","过流报警");
                        jsonObject.put("content","过流报警 报警值: A:"+aPhaseCurrentAlarmValueDouble+"A B:"+bPhaseCurrentAlarmValueDouble+"A C:"+cPhaseCurrentAlarmValueDouble+"A");
                        mapList.add(jsonObject);
                    }
                }else if (!alarmTypeCode.substring(4,8).equals("0000")){
                    //报警类型转二进制
                    String alarm = hexToBinaryString(alarmTypeCode.substring(4,8));

                    if (alarm.charAt(0) == '1'){

                    }
                    if (alarm.charAt(1) == '1'){

                    }
                    if (alarm.charAt(2) == '1'){
                        jsonObject = new JSONObject();
                        jsonObject.put("alarmTypeName","温度1报警");
                        jsonObject.put("content","A相过温报警 报警值:"+aPhaseTemperatureAlarmValueDouble+"℃");
                        mapList.add(jsonObject);
                    }
                    if (alarm.charAt(3) == '1'){

                    }
                    if (alarm.charAt(4) == '1'){

                    }
                    if (alarm.charAt(5) == '1'){
                        jsonObject = new JSONObject();
                        jsonObject.put("alarmTypeName","欠压报警");
                        jsonObject.put("content","A相欠压报警 报警值:"+aPhaseUnderVoltageAlarmValueDouble+"V");
                        mapList.add(jsonObject);
                    }
                    if (alarm.charAt(6) == '1'){
                        jsonObject = new JSONObject();
                        jsonObject.put("alarmTypeName","过压报警");
                        jsonObject.put("content","A相过压报警 报警值:"+aPhaseOvervoltageAlarmValueDouble+"V");
                        mapList.add(jsonObject);
                    }
                    if (alarm.charAt(7) == '1'){
                        jsonObject = new JSONObject();
                        jsonObject.put("alarmTypeName","过流报警");
                        jsonObject.put("content","A相过流报警 报警值:"+aPhaseCurrentAlarmValueDouble+"A");
                        mapList.add(jsonObject);
                    }
                }else if (!alarmTypeCode.substring(8,12).equals("0000")){
                    //报警类型转二进制
                    String alarm = hexToBinaryString(alarmTypeCode.substring(8,12));

                    if (alarm.charAt(0) == '1'){

                    }
                    if (alarm.charAt(1) == '1'){

                    }
                    if (alarm.charAt(2) == '1'){
                        jsonObject = new JSONObject();
                        jsonObject.put("alarmTypeName","温度2报警");
                        jsonObject.put("content","B相过温报警 报警值:"+bPhaseTemperatureAlarmValueDouble+"℃");
                        mapList.add(jsonObject);
                    }
                    if (alarm.charAt(3) == '1'){

                    }
                    if (alarm.charAt(4) == '1'){

                    }
                    if (alarm.charAt(5) == '1'){
                        jsonObject = new JSONObject();
                        jsonObject.put("alarmTypeName","欠压报警");
                        jsonObject.put("content","B相欠压报警 报警值:"+bPhaseUnderVoltageAlarmValueDouble+"V");
                        mapList.add(jsonObject);
                    }
                    if (alarm.charAt(6) == '1'){
                        jsonObject = new JSONObject();
                        jsonObject.put("alarmTypeName","过压报警");
                        jsonObject.put("content","B相过压报警 报警值:"+bPhaseOvervoltageAlarmValueDouble+"V");
                        mapList.add(jsonObject);
                    }
                    if (alarm.charAt(7) == '1'){
                        jsonObject = new JSONObject();
                        jsonObject.put("alarmTypeName","过流报警");
                        jsonObject.put("content","B相过流报警 报警值:"+cPhaseCurrentAlarmValueDouble+"A");
                        mapList.add(jsonObject);
                    }
                }else if (!alarmTypeCode.substring(12,16).equals("0000")){
                    //报警类型转二进制
                    String alarm = hexToBinaryString(alarmTypeCode.substring(12,16));

                    if (alarm.charAt(0) == '1'){

                    }
                    if (alarm.charAt(1) == '1'){

                    }
                    if (alarm.charAt(2) == '1'){
                        jsonObject = new JSONObject();
                        jsonObject.put("alarmTypeName","温度3报警");
                        jsonObject.put("content","C相过温报警 报警值:"+cPhaseTemperatureAlarmValueDouble+"℃");
                        mapList.add(jsonObject);
                    }
                    if (alarm.charAt(3) == '1'){

                    }
                    if (alarm.charAt(4) == '1'){

                    }
                    if (alarm.charAt(5) == '1'){
                        jsonObject = new JSONObject();
                        jsonObject.put("alarmTypeName","欠压报警");
                        jsonObject.put("content","C相欠压报警 报警值:"+cPhaseUnderVoltageAlarmValueDouble+"V");
                        mapList.add(jsonObject);
                    }
                    if (alarm.charAt(6) == '1'){
                        jsonObject = new JSONObject();
                        jsonObject.put("alarmTypeName","过压报警");
                        jsonObject.put("content","C相过压报警 报警值:"+cPhaseOvervoltageAlarmValueDouble+"V");
                        mapList.add(jsonObject);
                    }
                    if (alarm.charAt(7) == '1'){
                        jsonObject = new JSONObject();
                        jsonObject.put("alarmTypeName","过流报警");
                        jsonObject.put("content","C相过流报警 报警值:"+cPhaseCurrentAlarmValueDouble+"A");
                        mapList.add(jsonObject);
                    }
                }
                if (mapList.size() > 0){
                    //推送报警数据
                    for (JSONObject jsonO :mapList){
                        waterPumpAsyncService.sendOpenAlarm(deviceNumber+add, DateUtil.now(),jsonO.getStr("alarmTypeName"),jsonO.getStr("content"));
                        //异步更新设备状态
                        jsonObject.put("state","2");
                        jsonObject.put("number",deviceNumber+add);
                        asyncService.updateDeviceCurrentState(jsonObject);
                    }

                }else {
                    //异步更新设备状态
                    jsonObject.put("state","1");
                    asyncService.updateDeviceCurrentState(jsonObject);
                }
                JSONArray jsonArray = new JSONArray();
                jsonArray.add(getNormalItem("A相电压", aPhaseOvervoltageAlarmValueDouble));
                jsonArray.add(getNormalItem("B相电压", bPhaseOvervoltageAlarmValueDouble));
                jsonArray.add(getNormalItem("C相电压", bPhaseOvervoltageAlarmValueDouble));
                jsonArray.add(getNormalItem("A相电流", aPhaseCurrentAlarmValueDouble));
                jsonArray.add(getNormalItem("B相电流", aPhaseCurrentAlarmValueDouble));
                jsonArray.add(getNormalItem("C相电流", aPhaseCurrentAlarmValueDouble));
                jsonArray.add(getNormalItem("A线温度", aPhaseTemperatureAlarmValueDouble));
                jsonArray.add(getNormalItem("B线温度", bPhaseTemperatureAlarmValueDouble));
                jsonArray.add(getNormalItem("C线温度", cPhaseTemperatureAlarmValueDouble));
                JSONObject jsonParamN = new JSONObject();
                jsonParamN.put("number",deviceNumber+add);
                jsonParamN.put("time",DateUtil.now());
                jsonParamN.put("paraminfo",jsonArray);
                fireFightingiot(jsonParamN, "firefightingiot_data", "normalInfo", "device");
            //平时数据
            }else if (functionCode.equals("11")){
                //开关状态 0000开  00ff关
                String switchStatus = c.substring(58,62);
                String openingState = "";
                if (switchStatus.equals("0000")){
                    openingState = "1";
                }else {
                    openingState = "2";
                }
                //更新空开分闸状态
                waterPumpAsyncService.updateOpeningState(deviceNumber+add,openingState);
                //A相电流
                String aPhaseCurrent = c.substring(115,119);
                double aPhaseCurrentDouble  = hexToInt(aPhaseCurrent)/100.0;
                //B相电流
                String bPhaseCurrent = c.substring(119,123);
                double bPhaseCurrentDouble  = hexToInt(bPhaseCurrent)/100.0;
                //C相电流
                String cPhaseCurrent = c.substring(123,127);
                double cPhaseCurrentDouble  = hexToInt(cPhaseCurrent)/100.0;
                //A相电压
                String phaseAVoltage = c.substring(127,131);
                double phaseAVoltageDouble  = hexToInt(phaseAVoltage)/100.0;
                //B相电压
                String phaseBVoltage = c.substring(131,135);
                double phaseBVoltageDouble  = hexToInt(phaseBVoltage)/100.0;
                //C相电压
                String phaseCVoltage = c.substring(135,139);
                double phaseCVoltageDouble  = hexToInt(phaseCVoltage)/100.0;
                //A相有功功率
                String aPhaseActivePower = c.substring(139,143);
                //B相有功功率
                String bPhaseActivePower = c.substring(143,147);
                //C相有功功率
                String cPhaseActivePower = c.substring(147,151);
                //A相无功功率
                String aPhaseReactivePower = c.substring(151,155);
                //B相无功功率
                String bPhaseReactivePower = c.substring(155,159);
                //C相无功功率
                String cPhaseReactivePower = c.substring(159,163);
                //A相温度
                String phaseATemperature = c.substring(163,167);
                double phaseATemperatureDouble  = hexToInt(phaseATemperature)/10.0;
                //B相温度
                String phaseBTemperature = c.substring(167,171);
                double phaseBTemperatureDouble  = hexToInt(phaseBTemperature)/10.0;
                //C相温度
                String phaseCTemperature = c.substring(171,175);
                double phaseCTemperatureDouble  = hexToInt(phaseCTemperature)/10.0;
                //A相功率因数
                String aPhasePowerFactor = c.substring(175,179);
                double aPhasePowerFactorDouble  = hexToInt(aPhasePowerFactor)/100.0;
                //B相功率因数
                String bPhasePowerFactor = c.substring(179,183);
                double bPhasePowerFactorDouble  = hexToInt(bPhasePowerFactor)/100.0;
                //C相功率因数
                String cPhasePowerFactor = c.substring(183,187);
                double cPhasePowerFactorDouble  = hexToInt(cPhasePowerFactor)/100.0;
                JSONArray jsonArray = new JSONArray();
                jsonArray.add(getNormalItem("A相电压", phaseAVoltageDouble));
                jsonArray.add(getNormalItem("B相电压", phaseBVoltageDouble));
                jsonArray.add(getNormalItem("C相电压", phaseCVoltageDouble));
                jsonArray.add(getNormalItem("A相电流", aPhaseCurrentDouble));
                jsonArray.add(getNormalItem("B相电流", bPhaseCurrentDouble));
                jsonArray.add(getNormalItem("C相电流", cPhaseCurrentDouble));
                jsonArray.add(getNormalItem("A线温度", phaseATemperatureDouble));
                jsonArray.add(getNormalItem("B线温度", phaseBTemperatureDouble));
                jsonArray.add(getNormalItem("C线温度", phaseCTemperatureDouble));
                jsonArray.add(getNormalItem("A相有功功率", aPhaseActivePower));
                jsonArray.add(getNormalItem("B相有功功率", bPhaseActivePower));
                jsonArray.add(getNormalItem("C相有功功率", cPhaseActivePower));
                JSONObject jsonParamN = new JSONObject();
                jsonParamN.put("number",deviceNumber+add);
                jsonParamN.put("time",DateUtil.now());
                jsonParamN.put("paraminfo",jsonArray);
                fireFightingiot(jsonParamN, "firefightingiot_data", "normalInfo", "device");
            }else if (functionCode.equals("20")){
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("name", "今日数据变化");
                jsonObject.put("value", 0);
                JSONArray jsonArray = new JSONArray();
                jsonArray.add(jsonObject);
                //rest调用平时数据添加接口：
                JSONObject jsonParamN = new JSONObject();
                jsonParamN.put("paraminfo", jsonArray);
                jsonParamN.put("number", deviceNumber);
                jsonParamN.put("time", DateUtil.now());
                fireFightingiot(jsonParamN, "firefightingiot_data", "normalInfo", "device");
            }else {
                return RespEntity.fail("数据不正确!");
            }
        }
        return RespEntity.success();
    }

    /**
     * @Author YangYu
     * @Description 平时数据处理
     * @Date Created in 14:53 2021/11/30
     * @Param [name, value]
     */

    public JSONObject getNormalItem(String name, Object value) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", name);
        jsonObject.put("value", value);
        return jsonObject;
    }
    /**
     * @Author YangYu
     * @Description 十六进制转二进制字符串
     * @Date Created in 14:07 2021/11/24
     * @Param [hex]
     */

    public static String hexToBinaryString(String hex) {
        String str = Integer.toBinaryString(Integer.parseInt(hex,16));
        if(str.length() == 1){
            str = "0000000" + str;
        }else if(str.length() == 2){
            str = "000000" + str;
        }else if(str.length() == 3){
            str = "00000" + str;
        }else if(str.length() == 4){
            str = "0000" + str;
        }else if(str.length() == 5){
            str = "000" + str;
        }else if(str.length() == 6){
            str = "00" + str;
        }else if(str.length() == 8){
            str = "0" + str;
        }
        return str;
    }
    /**
     * @Author YangYu
     * @Description 十六进制转十进制
     * @Date Created in 15:08 2021/11/29
     * @Param [hex]
     */

    public int hexToInt(String hex){
        if (hex.length() < 1){
            return 0;
        }else {
            return Integer.parseInt(hex,16);
        }
    }

    public static void main(String[] args) {
       String s = "123456";
       String add = s.substring(s.length()-2,s.length());
        System.out.println(add);

        //System.out.println(getCRC(hexToBytes("0119000100FF")));
        String ss = Integer.toHexString(Integer.parseInt("15"));

        if (ss.length() <2){
            ss ="0"+ss;
        }
        System.out.println(ss);
        System.out.println(Integer.toHexString(Integer.parseInt("56")));

    }


}
